package com.hourse.app.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

public class DrawImageUtil {

    static Random r = new Random();
    static int width = 220;// 图片宽
    static int height = 180;// 图片高
    static int lineSize = 40;// 干扰线数量

    public static void drawPlusImage(String firstNum, String secondNum, String operator, OutputStream os) throws IOException {

        BufferedImage bimg = new BufferedImage(width,height,1);

        Graphics2D graphics = (Graphics2D) bimg.getGraphics();

        graphics.setColor(Color.WHITE);//设置画笔颜色-验证码背景色

        graphics.fillRect(0, 0, width, height);//填充背景

        graphics.setFont(new Font("微软雅黑", Font.BOLD, 80));

        //数字和字母的组合

        String baseNumLetter ="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";

        //StringBuffer sBuffer = new StringBuffer();

        int x = 32;  //旋转原点的 x 坐标

        String ch = "";

        Random random = new Random();

        int degree = random.nextInt() % 30;  //角度小于30度
        graphics.setColor(getColor());
        //graphics.rotate(degree * Math.PI / 180, x, 45);
        graphics.drawString(firstNum, x, 120);
       // graphics.rotate(-degree * Math.PI / 180, x, 45);

        x+=44;
        graphics.setColor(getColor());
        degree = random.nextInt() % 30;  //角度小于30度
        //graphics.rotate(degree * Math.PI / 180, x, 45);
        graphics.drawString(operator, x, 120);
       // graphics.rotate(-degree * Math.PI / 180, x, 45);

        x+=44;
        graphics.setColor(getColor());
        degree = random.nextInt() % 30;  //角度小于30度
       // graphics.rotate(degree * Math.PI / 180, x, 45);
        graphics.drawString(secondNum, x, 120);
       // graphics.rotate(-degree * Math.PI / 180, x, 45);

        x+=44;
        graphics.setColor(getColor());
        degree = random.nextInt() % 30;  //角度小于30度
       // graphics.rotate(degree * Math.PI / 180, x, 45);
        graphics.drawString("=", x, 120);
       // graphics.rotate(-degree * Math.PI / 180, x, 45);



        /*for(int i = 0;i < 4;i++){

            graphics.setColor(getColor());
            //设置字体旋转角度
            degree = random.nextInt() % 30;  //角度小于30度
            int dot = random.nextInt(baseNumLetter.length());
            ch = baseNumLetter.charAt(dot) + "";
            //sBuffer.append(ch);
            //正向旋转
            graphics.rotate(degree * Math.PI / 180, x, 45);
            graphics.drawString(ch, x, 45);
            //反向旋转
            graphics.rotate(-degree * Math.PI / 180, x, 45);
            x += 48;
        }*/


        //画干扰线
        for (int i = 0; i < 36; i++) {
            // 设置随机颜色
            graphics.setColor(getColor());
            // 随机画线
            graphics.drawLine(random.nextInt(width), random.nextInt(height),
            random.nextInt(width), random.nextInt(height));
        }

        //添加噪点

        for (int i = 0; i < 160; i++) {

            int x1 = random.nextInt(width);

            int y1 = random.nextInt(height);

            graphics.setColor(getColor());

            graphics.fillRect(x1, y1, 2, 2);

        }

        ImageIO.write(bimg,"png",os);//输出图片流


    }
    static Color getColor(){
        return new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)) ;
    }

    private static  void  drowLine(Graphics g) {
        int x = r.nextInt(width);
        int y = r.nextInt(height);
        int xl = r.nextInt(13);
        int yl = r.nextInt(15);
        g.drawLine(x, y, x + xl, y + yl);
    }
}
